<?php

namespace App\Http\Resources\panyuan;

use App\model\panyuan\MyApply;
use App\model\Workshop;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Carbon;

class MyApplyCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'code'=>'success',
            'data'=>$this->collection->map(function($item){
                if (isset($item->workshop)&&$item->workshop){


                if(isset($item->workshop->type)&&$item->workshop->type==0){
                    $price=$item->workshop->rent!=0 ? $item->workshop->rent.Workshop::RENT  : '面议';
                }else{
                    $price=$item->workshop->price!=0 ? $item->workshop->rent.Workshop::SALE  : '面议';
                }
                return [
                    'workshop_id'=>$item->workshop->id,
                    'logo'=>$item->workshop->logo.Workshop::LOGO,
                    'title'=>$item->workshop->title,
                    'code'=>$item->code,
                    'area'=>$item->workshop->area,
                    'status'=>$item->status,
                    'price'=>$price,
                    'tags'=>$item->workshop->tags->reject(function($item,$key){
                        return $key>2;
                    }),
                    'status_desc'=>MyApply::$shenheMap[$item->status],
                    'type'=>$item->type,
                    'yezhu_time_start'=>Carbon::parse($item->created_at)->format('Y/m/d') ,
                    'yezhu_time_end'=>Carbon::parse($item->created_at)->addDays(1)->format('Y/m/d') ,
                    'pass_time'=>Carbon::parse($item->updated_at)->format('m/d'),
                    'pass_time1'=>Carbon::parse($item->updated_at)->format('Y/m/d'),
                    'commission_end_day'=>Carbon::parse($item->workshop->commission_end_day)->format('Y/m/d'),
                    'commission_start_day'=>Carbon::parse($item->workshop->commission_start_day)->format('Y/m/d'),
                    'owner'=>$item->workshop->owner ?? '',
                ];
                }
            })

        ];
    }
}
